package com.zkr.kqsx.service;

import com.zkr.kqsx.mapper.KQSXMapper;
import com.zkr.kqsx.model.*;
import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2021/1/13.
 */
@Service
public class KQSXServiceImpl implements KQSXService {

    @Autowired
    private KQSXMapper kqsxMapper;

    @Override
    public ResultVo queryPage(KQSXSelectVo kqsxSelectVo) {
        int count = kqsxMapper.queryCount(kqsxSelectVo);
        List<KQSXVo> kqsxVoList = kqsxMapper.queryPage(kqsxSelectVo);
        for (KQSXVo kqsxVo:kqsxVoList){
            Integer status =kqsxVo.getStatus()==true?1:0;
            kqsxVo.setStatusName(StatusEnum.getName(status));
           /* for (StatusEnum c:StatusEnum.values()){
                if (c.getValue().equals(status)){
                    kqsxVo.setStatusName(c.getName());
                }
            }*/
        }

        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(kqsxSelectVo.getPageIndex());
        allRecords.setDataList(kqsxVoList);
        allRecords.setPageSize(kqsxSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);


    }

    @Override
    @Transactional
    public ResultVo add(@Valid KQSXAddVo kqsxAddVo) {
        //验证库区编码
        KQSXVo kqsxCode = kqsxMapper.queryCode(kqsxAddVo.getCode());
        if (kqsxCode !=null){
            return ResultBuilderVo.error("库区属性编码已经存在");
        }
        KQSXVo kqsxName = kqsxMapper.queryName(kqsxAddVo.getName());
        if (kqsxName != null){
            return ResultBuilderVo.error("库区属性名称已经存在");
        }
        KQSXVo kqsxVo = new KQSXVo();
        kqsxVo.setCreateName("李四");
        kqsxVo.setCreateTime(new Date());
        kqsxVo.setUpdateName("李四");
        kqsxVo.setUpdateTime(new Date());
        //把左边赋值到右边
        BeanUtils.copyProperties(kqsxAddVo,kqsxVo);
        //入库
         kqsxMapper.add(kqsxVo);
        return ResultBuilderVo.success();

    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid KQSXUpdateStatusVo kqsxUpdateStatusVo) {
        KQSXVo kqsxVo = new KQSXVo();
        BeanUtils.copyProperties(kqsxUpdateStatusVo,kqsxVo);
        kqsxVo.setUpdateTime(new Date());
        kqsxVo.setUpdateName("李四");
        kqsxMapper.updateStatus(kqsxVo);
        return ResultBuilderVo.success();

    }

    @Override
    @Transactional
    public ResultVo update(@Valid KQSXUpdateVo kqsxUpdateVo) {
        KQSXVo kqsxName = kqsxMapper.queryName(kqsxUpdateVo.getName());
        if (kqsxName != null){
            if (!(kqsxName.getSid().equals(kqsxName.getSid()))){
                return ResultBuilderVo.error("库区属性名称已经存在");
            }
        }
        KQSXVo kqsxVo = new KQSXVo();
        BeanUtils.copyProperties(kqsxUpdateVo,kqsxVo);
        kqsxVo.setUpdateTime(new Date());
        kqsxVo.setUpdateName("李四");
        kqsxMapper.update(kqsxVo);
        return ResultBuilderVo.success();
    }

   /*
    @Override
    @Transactional
    public ResultVo update(@Valid KuFangUpdateVo kuFangUpdateVo) {
        KQSXVo kufangname = kuFangMapper.queryName(kuFangUpdateVo.getName());
        if (kufangname != null){
            if (!(kufangname.getId().equals(kuFangUpdateVo.getId()))){
                return ResultBuilderVo.error("库房标识名称已经存在");
            }

        }

        KQSXVo kuFangVo1 = new KQSXVo();
        BeanUtils.copyProperties(kuFangUpdateVo,kuFangVo1);
        kuFangVo1.setUpdateTime(new Date());
        kuFangVo1.setUpdateName("李四");
        kuFangMapper.update(kuFangVo1);

        return ResultBuilderVo.success();


    }*/
}
